package com.tensquare.recruit.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.recruit.pojo.Enterprise;
import com.tensquare.recruit.service.EnterpriseService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/enterprise")
@CrossOrigin
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;
    @PostMapping
    public Result save(@RequestBody Enterprise enterprise){
        enterpriseService.save(enterprise);
        return new Result(true, StatusCode.OK,"ok");
    }
    //  GET/enterprise 标签全部列表
    @GetMapping
    public Result findAll(){
        List<Enterprise> enterprises=enterpriseService.findAll();
        return new Result(true,StatusCode.OK,"ok",enterprises);
    }
    // GET/enterprise/{enterpriseId}根据ID查询
    @GetMapping("/{enterpriseId}")
    public Result findByEnterpriseId(@PathVariable String enterpriseId) {
        Enterprise enterprise=enterpriseService.findById(enterpriseId);
        return new Result(true,StatusCode.OK,"ok",enterprise);
    }
    // PUT/enterprise/{enterpriseId}修改标签
    @PutMapping("{enterpriseId}")
    public Result findByEnterpriseId(@PathVariable String enterpriseId,@RequestBody Enterprise enterprise) {
        enterprise.setId(enterpriseId);
        enterpriseService.updateByEnterpriseId(enterprise);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/enterprise/{enterpriseId}根据ID删除
    @DeleteMapping("{enterpriseId}")
    public  Result deleteByEnterpriseId(@PathVariable String enterpriseId){
        enterpriseService.deleteByEnterpriseId(enterpriseId);
        return new Result(true,StatusCode.OK,"ok");
    }
    /*
    * POST
    /enterprise/search/{page}/{size}
    标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page pageData=enterpriseService.findByPage(map,page,size);
        PageResult pageResult=new PageResult(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * POST
    /enterprise/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Enterprise> list=enterpriseService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
    }
    /*
    * GET/enterprise/search/hotlist热门企业列表*/

    @GetMapping("/search/hotlist")
    public Result findByhotlist( ){
        return new Result(true,StatusCode.OK,"ok",enterpriseService.findByHot());
    }

}
